package com.hertz.iris.view.matchalarm;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.actionbarsherlock.app.SherlockFragment;
import com.hertz.iris.R;
import com.hertz.iris.pojo.MatchAlarm;
import com.hertz.iris.ui.SeekArc;
import com.hertz.iris.util.BundleKeys;
import com.hertz.iris.util.Utils;

public class MatchAlarmConditionSelectorFragment extends SherlockFragment {
	
	@InjectView(R.id.period_picker)
	SeekArc periodPicker;
	@InjectView(R.id.remain_picker)
	SeekArc remainPicker;
	@InjectView(R.id.period)
	TextView periodText;
	@InjectView(R.id.remain)
	TextView remainText;
	
	private String[] periods;
	private int[] remainNba;
	
	private byte period;
	private byte remain;
	
	private OnMatchAlarmConditionSelectListener myCallback;
	
	private MatchAlarm matchAlarm;
	
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			myCallback = (OnMatchAlarmConditionSelectListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnMatchAlarmConditionSelectListener");
        }
	}
	
	@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) { 
		super.onCreateView(inflater, container, savedInstanceState);
		View v = inflater.inflate(R.layout.match_alarm_condition_selector_frag, container, false);
		ButterKnife.inject(this,v);
		initView();
		remainPicker.setOnSeekArcChangeListener(new SeekArc.OnSeekArcChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekArc seekArc) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onStartTrackingTouch(SeekArc seekArc) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onProgressChanged(SeekArc seekArc, int progress,
					boolean fromUser) {
				remain = Integer.valueOf(remainNba[progress]).byteValue();
				remainText.setText(String.format(getResources().getString(R.string.remain),remain ));
				myCallback.matchAlarmConditionSelectListener(period, remain);
			}
		});
		
		periodPicker.setOnSeekArcChangeListener(new SeekArc.OnSeekArcChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekArc seekArc) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekArc seekArc) {
				
			}
			
			@Override
			public void onProgressChanged(SeekArc seekArc, int progress,
					boolean fromUser) {
				if(period == 0){
					remainText.setVisibility(View.GONE);
					remainPicker.setVisibility(View.GONE);
				}else{
					remainText.setVisibility(View.VISIBLE);
					remainPicker.setVisibility(View.VISIBLE);
				}
				period = Integer.valueOf(progress).byteValue();
				periodText.setText(getResources().getStringArray(R.array.period)[period]);	
				myCallback.matchAlarmConditionSelectListener(period, remain);
			}
		});
		return v;
	}
	
	private void initView(){
		periods = getResources().getStringArray(R.array.period);
		remainNba = getResources().getIntArray(R.array.remain_nba);
		remainPicker.setMax(remainNba.length - 1);
		periodPicker.setMax(periods.length - 1);
		
		Bundle bundle = getArguments();
		matchAlarm = (MatchAlarm)bundle.getSerializable(BundleKeys.MatchAlarmBundleKeys.MATCH_ALARM_KEY);
		
		periodText.setText(getResources().getStringArray(R.array.period)[matchAlarm.getPeriod()]);
		remainText.setText(String.format(getResources().getString(R.string.remain), matchAlarm.getRemain()));
		
		
		remain = matchAlarm.getRemain();
		period = matchAlarm.getPeriod();
		
		periodPicker.setProgress(period);
		remainPicker.setProgress(Utils.indexInArr(remainNba, matchAlarm.getRemain()));
		if(period == 0){
			remainText.setVisibility(View.GONE);
			remainPicker.setVisibility(View.GONE);
		}
	}
	
	public interface OnMatchAlarmConditionSelectListener{
		public void matchAlarmConditionSelectListener(byte period,byte remain);
	}
	
}
